#include "elevatorwidget.h"
#include "./ui_elevatorwidget.h"

#include<QTimer>
#include<QPainter>
#include<map>
using namespace std;

ElevatorWidget::ElevatorWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::ElevatorWidget)
{
    ui->setupUi(this);
    // this->setFixedSize(800,600);

    elevatorSystem = new ElevatorSystem;
    timer[0] = new QTimer(this);
    // timer[1] = new QTimer(this);

    connect(timer[0],&QTimer::timeout,this,&ElevatorWidget::updateStatus);

    timer[0]->start(1000); // 每1000毫秒（1秒）触发一次
    // timer[1]->start(1000);
    QToolButton* button = this->findChild<QToolButton*>(m[9]);
    // if(button)
    //     qDebug()<<"aaa";
    // else
    //     qDebug()<<"bbb";

}

ElevatorWidget::~ElevatorWidget()
{
    delete ui;
}




void ElevatorWidget::updateStatus()
{
    int a =elevatorSystem->elevator[0]->move();
    QToolButton* button = this->findChild<QToolButton*>(m[a]);
    if(button) button->setStyleSheet("background-color: white;");
    int b =elevatorSystem->elevator[1]->move();
    QToolButton* button1 = this->findChild<QToolButton*>(m[b]);
    if(button1) button1->setStyleSheet("background-color: white;");
    int e1CurrentFloor = elevatorSystem->elevator[0]->getCurrentFloor();
    int e2CurrentFloor = elevatorSystem->elevator[1]->getCurrentFloor();
    ui->label->move(0,250-e1CurrentFloor*25);
    ui->label->setText(QString::number(e1CurrentFloor));
    ui->label_2->move(0,250-e2CurrentFloor*25);
    ui->label_2->setText(QString::number(e2CurrentFloor));
}

void ElevatorWidget::paintEvent(QPaintEvent *p)//页面加载，update
{
    QPainter painter(this);
    // painter.drawLine(40,65,90,65);
}


void ElevatorWidget::on_pushButton_19_clicked()
{
    elevatorSystem->distribute(1,UP);
}

void ElevatorWidget::on_pushButton_17_clicked()
{
    elevatorSystem->distribute(2,UP);
}

void ElevatorWidget::on_pushButton_15_clicked()
{
    elevatorSystem->distribute(3,UP);
}


void ElevatorWidget::on_pushButton_13_clicked()
{
    elevatorSystem->distribute(4,UP);
}
void ElevatorWidget::on_pushButton_11_clicked()
{
    elevatorSystem->distribute(5,UP);
}
void ElevatorWidget::on_pushButton_9_clicked()
{
    elevatorSystem->distribute(6,UP);
}
void ElevatorWidget::on_pushButton_7_clicked()
{
    elevatorSystem->distribute(7,UP);
}
void ElevatorWidget::on_pushButton_5_clicked()
{
    elevatorSystem->distribute(8,UP);
}
void ElevatorWidget::on_pushButton_3_clicked()
{
    elevatorSystem->distribute(9,UP);
}
void ElevatorWidget::on_pushButton_clicked()
{
    elevatorSystem->distribute(10,UP);
}

void ElevatorWidget::on_toolButton_11_clicked()
{
    ui->toolButton_11->setStyleSheet("background-color: red;");
}



void ElevatorWidget::on_pushButton_2_clicked()
{
    elevatorSystem->distribute(10,DOWN);
}


void ElevatorWidget::on_pushButton_4_clicked()
{
    elevatorSystem->distribute(9,DOWN);
}

void ElevatorWidget::on_pushButton_6_clicked()
{
    elevatorSystem->distribute(8,DOWN);
}


void ElevatorWidget::on_pushButton_8_clicked()
{
    elevatorSystem->distribute(7,DOWN);
}
void ElevatorWidget::on_pushButton_10_clicked()
{
    elevatorSystem->distribute(6,DOWN);
}


void ElevatorWidget::on_pushButton_12_clicked()
{
    elevatorSystem->distribute(5,DOWN);
}
void ElevatorWidget::on_pushButton_14_clicked()
{
    elevatorSystem->distribute(4,DOWN);
}


void ElevatorWidget::on_pushButton_16_clicked()
{
    elevatorSystem->distribute(3,DOWN);
}
void ElevatorWidget::on_pushButton_18_clicked()
{
    elevatorSystem->distribute(2,DOWN);
}
void ElevatorWidget::on_pushButton_20_clicked()
{
    elevatorSystem->distribute(1,DOWN);
}


void ElevatorWidget::on_toolButton_2_clicked()
{
    ui->toolButton_2->setStyleSheet("background-color: red;");
    elevatorSystem->distribute(9,UP);
}


